<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/12/26
 * Time: 13:23
 */
namespace app\admin\controller;
use app\common\lib\db\DbCommon;
use app\common\lib\exception\ApiException;
use think\Db;
use think\Request;
class Comment extends Base{
    public function index(){
        if (request()->isAjax()) {
            extract(input()); //将接收的参数赋值给变量
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $map = array();
            //关键字
            if(isset($keyword) && $keyword!="")
            {
                $map['content'] = ['like',"%" . $keyword . "%"];
            }
            try{
                $count = Db::name('comment')->where($map)->count();  //总条目数据
                $lists = Db::name('comment')->page($NowPage,$limits)->where($map)->select();  //当前页
            }catch (\Exception $e) {
                return json(['code' => 500, 'msg' => $e->getMessage()]);
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    public function update()
    {
        if(\request()->isPOST()){
            $data = input('post.');
            $res = Model('UUser')->update_user_msg($data['user_id'],$data);
            if(!$res) {
                throw new ApiException('没有做任何更改','200');
            }
            return show('200','已更新',$res);
        }else {
            $comment_id = Request::instance()->param('comment_id');
            $data = Model('Comment')->where(['comment_id' => $comment_id])->find();
            $media = DbCommon::get_media_msg($data['f_id'],$data['type']);
            if($data['type'] == 1) {
                $media_info = [
                    'name' => $media['0']['video_name'],
                    'user' => $media['0']['video_performer'],
                    'is_down' => $media['0']['is_down'],
                    'is_vip' => $media['0']['is_vip'],
                    'mode' => $media['0']['video_modular_name'],
                    'mode_type' => $media['0']['video_type_name'],
                ];
            }elseif ($data['type'] == 2) {
                $media_info = [
                    'name' => $media['0']['music_name'],
                    'user' => $media['0']['music_performer'],
                    'is_down' => $media['0']['is_down'],
                    'is_vip' => $media['0']['is_vip'],
                    'mode' => $media['0']['video_modular_name'],
                    'mode_type' => $media['0']['music_type_name'],
                ];
            }else{
                $media_info = [
                    'msg' => '参数错误'
                ];
            }
            $this->assign([
                'data' => $data,
                'media_info' => $media_info
            ]);
//        halt($data);
            return $this->fetch();
        }
    }

    /**
     * Ajax抛送删除
     * @return \think\response\Json
     */
    public function del()
    {
        $comment_id = Request::instance()->param('id');
        $res = Model('Comment')->where(['comment_id'=>$comment_id])->delete();
        if(!$res) {
            return json(['code'=>500,'msg'=>'数据库错误']);
        }
        return json(['code'=>200,'msg'=>'已删除']);
    }

    /**
     * 查看是否已经删除，而不是删除继续点击删除
     * @param number $user_id 用户id
     * @return bool $is_del
     */
    public function is_del($user_id)
    {
        $res = Model('UUser')->get(['user_id' => $user_id,'user_status' => '4']);
        if(!$res) {
            return false;
        }
        return true;
    }
}